package com.zretc.support.dao;

import com.zretc.support.entity.Answer;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;
@Repository
public interface AnswerDao extends JpaRepository<Answer,Integer>, JpaSpecificationExecutor<Answer>,
        CrudRepository<Answer,Integer>, Serializable {

    List<Answer> findAnswerByExamIdAndUserId(int examId,String userId);

    Answer findAnswerByTopicIdAndUserId(String topicId,String userId);

    /**
     * 精确查找
     *添加部分
     * @param id
     * @return
     */
    @Query(value = "select * from answer  where examId = ?1", nativeQuery = true)
    Answer getByExamIds(long examId);

    /**
     * 考试类型编号->参加考试的学生分页
     * @param keywords
     * @param pageable
     * @return
     */
    @Query(value = "select * from Answer where topicId = ?1",nativeQuery = true)
    Page<Answer> page(String keywords, Pageable pageable);

    /**
     *更新答题分数
     * @param socre
     * @param answerId
     * @return
     */
    @Modifying
    @Transactional
    @Query(value = "update Answer set socre = ?1 where answerId =?2")
    int updScore(double socre, Integer answerId);
}
